from dataparsers import *
from cStringIO import StringIO

def login_request(username):
    return '\x01'+int(11)+string16(username)+long(0)+byte(0)

def parse_login_request(packet):
    packet = StringIO(packet[1:])
    result = {}
    result['Protocol Version'] = parse_int(packet)
    result['Username'] = parse_string16(packet)
    result['Map Seed'] = parse_long(packet)
    result['Dimension'] = parse_byte(packet)
    return result
    

def handshake(username):
    return '\x02'+string16(username)

def parse_handshake(packet):
    packet = StringIO(packet[1:])
    return {'Connection Hash':parse_string16(packet)}

def chat_message(message):
    return '\x03'+string16(message)

def parse_chat_message(packet):
    packet = StringIO(packet[1:])
    return {'Message':parse_string16(packet)}

def parse_time_update(packet):
    packet = StringIO(packet[1:])
    return {'Time':parse_long(packet)}

def parse_kick(packet):
    packet = StringIO(packet[1:])
    return {'Reason':parse_string16(packet)}
